export const PhoneCodes = new Mongo.Collection('phone_codes');
PhoneCodes.allow({
    insert: function () {
        return true;
    },
    update: function(userId, post) { return ownsDocument(userId, post); },
    remove: function(userId, post) { return ownsDocument(userId, post); }
});
PhoneCodes.schema = new SimpleSchema({
    telephone: {
        label: '手机号',
        type: String,
    },
    verification_code: {
        label: '验证码',
        type: String,
        optional: true
    },
    type: {
        label: '操作类型',
        type: String,
        optional: true
    },
    createdAt: {
        label: '创建时间',
        type: Date,
        optional: true,
        autoValue() {
            if (this.isInsert) {
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
            return this.unset();  // Prevent user from supplying their own value
        }
    }
});
PhoneCodes.attachSchema(PhoneCodes.schema);
// PhoneCodes._ensureIndex({ "createdAt": 1 }, { expireAfterSeconds: 300 });

Meteor.methods({
    codeInsert: function(commentAttributes) {
        comment = _.extend(commentAttributes, {
            createdAt: new Date()
        });
        return PhoneCodes.insert(comment);
    },
    findByPhone: function(phone, code) {
        console.log('phone : ', phone);
        console.log('code : ', code);
        var pc = PhoneCodes.findOne({telephone: phone, verification_code: code});
        return pc;
        
    }
});